package com.starblink.home.subscription.guide

import android.animation.ObjectAnimator
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.extension.navigationTo
import com.starblink.basic.route.RoutePage
import com.starblink.home.databinding.GuideHomeSubsBinding
import razerdp.basepopup.BasePopupWindow

/**
 * 订阅Tab引导Pop
 *  Create by yuanl at 2023/10/10
 **/
class SubsGuidePop(context: Context) : BasePopupWindow(
    context,
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT
) {

    private val binding = GuideHomeSubsBinding.inflate(LayoutInflater.from(context))

    private var shakeAnim: ObjectAnimator? = null

    init {
        contentView = binding.root
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        setBackgroundColor(0x4D000000)
        binding.btn.setOnClickListener {
            dismiss()
        }
        binding.btnAdd.setOnClickListener {
            navigationTo(RoutePage.Search.PAGE_SEARCH)
            dismiss()
        }
    }

    override fun onBeforeShow(): Boolean {
        return super.onBeforeShow()
    }

    override fun onShowing() {
        super.onShowing()
        shakeAnim = ObjectAnimator.ofFloat(binding.layoutContent, "translationY", 0f, 10.dp.toFloat()).apply {
            duration = 500L
            repeatMode = ObjectAnimator.REVERSE
            repeatCount = 1
            interpolator = AccelerateDecelerateInterpolator()
        }
        shakeAnim?.start()
    }

    override fun onDismiss() {
        shakeAnim?.cancel()
        super.onDismiss()
    }


    override fun showPopupWindow(anchorView: View?) {
        super.showPopupWindow(anchorView)
        offsetY -= (anchorView?.measuredHeight?:0) + 4.dp
    }

}